﻿using System;
using System.Configuration;

namespace TemplateFoundation.Handlers.DynamicImageProcessor
{
    /// <summary>
    /// Configuration class for the Dynamic Image Processor
    /// </summary>
    public class ProcessorConfig : ConfigurationSection
    {
        /// <summary>
        /// Gets or sets whether high quality rendering is used
        /// </summary>
        /// <remarks>This produces larger file sizes</remarks>
        [ConfigurationProperty("HighQuality", DefaultValue="false", IsRequired=false)]
        public Boolean HighQuality
        {
            get
            {
                return (Boolean)(this["HighQuality"] ?? false);
            }
            set
            {
                this["HighQuality"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the default MIME type of images
        /// </summary>
        [ConfigurationProperty("DefaultMimeType", DefaultValue="image/jpeg", IsRequired=false)]
        public string DefaultMimeType
        {
            get
            {
                return (string)this["DefaultMimeType"];
            }
            set
            {
                this["DefaultMimeType"] = value;
            }
        }

        /// <summary>
        /// Gets or sets image to use for failed image requests
        /// </summary>
        [ConfigurationProperty("ImageNotFound")]
        public ImageNotFoundConfigurationElement ImageNotFound
        {
            get
            {
                return (ImageNotFoundConfigurationElement)this["ImageNotFound"];
            }
            set
            {
                this["ImageNotFound"] = value;
            }
        }

        /// <summary>
        /// Gets or sets cache settings
        /// </summary>
        [ConfigurationProperty("Cache")]
        public CacheConfigurationElement Cache
        {
            get
            {
                return (CacheConfigurationElement)this["Cache"];
            }
            set
            {
                this["Cache"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the image to use for failed image requests
        /// </summary>
        public class ImageNotFoundConfigurationElement : ConfigurationElement
        {
            /// <summary>
            /// Image to display when an image cannot be found
            /// </summary>
            [ConfigurationProperty("FileName", DefaultValue="", IsRequired=false)]
            public string FileName
            {
                get
                {
                    return (string)this["FileName"];
                }
                set
                {
                    this["FileName"] = value;
                }
            }

            /// <summary>
            /// Gets or sets whether a feature to include a specific image when a image request fails
            /// </summary>
            [ConfigurationProperty("Enabled", DefaultValue="false", IsRequired=false)]
            public Boolean Enabled
            {
                get
                {
                    return (Boolean)(this["Enabled"] ?? false);
                }
                set
                {
                    this["Enabled"] = value;
                }
            }
        }

        /// <summary>
        /// Class for specifying cache settings
        /// </summary>
        public class CacheConfigurationElement : ConfigurationElement
        {
            /// <summary>
            /// Gets or sets the directory in which to store cached images
            /// </summary>
            [ConfigurationProperty("CacheDirectory", DefaultValue="", IsRequired=false)]
            public string CacheDirectory
            {
                get
                {
                    return (string)this["CacheDirectory"];
                }
                set
                {
                    this["CacheDirectory"] = value;
                }
            }

            /// <summary>
            /// Gets or sets whether image cache should be enabled
            /// </summary>
            [ConfigurationProperty("Enabled", DefaultValue="false", IsRequired=false)]
            public Boolean Enabled
            {
                get
                {
                    return (Boolean)(this["Enabled"] ?? false);
                }
                set
                {
                    this["Enabled"] = value;
                }
            }

            /// <summary>
            /// Gets or sets whether image cache should be automatically refreshed by checking last write time of original file upon request
            /// </summary>
            [ConfigurationProperty("AutomaticRefresh", DefaultValue="true", IsRequired=false)]
            public Boolean AutomaticRefresh
            {
                get
                {
                    return (Boolean)(this["AutomaticRefresh"] ?? false);
                }
                set
                {
                    this["AutomaticRefresh"] = value;
                }
            }
        }
    }

}
